<template>
  <div class="user">
    <div class="top">
      <queryInput
        id="userName"
        name="用户名"
        :value="query.userName"
        @update:value="handleUpdate"
      />
      <queryInput
        id="name"
        name="姓名"
        :value="query.name"
        @update:value="handleUpdate"
      />
      <div>
        <el-button type="primary" @click="handle_query">查询</el-button>
        <el-button @click="handle_resetting">重置</el-button>
      </div>
    </div>
    <div class="cont">
      <div class="cont_top">
        <div class="total_box">
          用户总计：
          <span>{{ total }}</span>

          <p>
            已选择
            <span>{{ selectionNum }}</span>
            项
          </p>
        </div>
        <div class="operate">
          <!-- <el-button>导出</el-button> -->
        </div>
      </div>
      <el-table
        v-loading="loading"
        style="height: 100%"
        :data="tableData"
        row-key="bm"
        max-height="660"
        :header-cell-style="{
          backgroundColor: '#f7f8fa',
          color: '#8c8c8c',
          fontWeight: 400,
          cursor: 'pointer',
        }"
        :cell-style="{ color: '#000' }"
        @select="handle_select"
        @select-all="handle_select"
      >
        <template #empty>
          <el-empty :image-size="100" description="暂无数据" />
        </template>
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column type="index" label="序号" width="100" align="center">
          <template #default="scope">{{ scope.row.pid }}</template>
        </el-table-column>
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="fullname" label="姓名" />
        <el-table-column prop="organizitionName" label="所属机构" />
        <!-- <el-table-column prop="userSource" label="用户来源" /> -->
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag v-if="scope.row.status" type="success">启用</el-tag>
            <el-tag v-else type="info">停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div class="cont_button">
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[15, 20, 30, 40]"
          layout="->,slot, sizes, prev, pager, next, jumper"
          :total="total"
          @change="handle_page"
        >
          <div class="total">共{{ Math.ceil(total / pageSize) }}页</div>
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import { userpage } from "@/api/basic";
import { useStore } from "@/store";
import { onMounted, reactive, ref } from "vue";

const store = useStore();

const loading = ref(false);
const total = ref(0);
const pageNo = ref(1);
const pageSize = ref(15);
const selectionNum = ref(0);
const query = reactive({
  userName: "",
  name: "",
});

const tableData = ref([]);

//更新查询数据
const handleUpdate = ({ key, value }) => {
  query[key] = value;
  if (!value) {
    get_userpage();
  }
};
//查询
const handle_query = () => {
  get_userpage();
};

//查询--重置
const handle_resetting = () => {
  query.userName = "";
  query.name = "";
  get_userpage();
};

//表格---勾选数据行
const handle_select = (selection) => {
  selectionNum.value = selection.length;
};

//分页
const handle_page = (currentPage, pageSize) => {
  get_userpage();
};

//获取用户
let time = ref(null);
const get_userpage = async () => {
  loading.value = true;

  const res = await userpage({
    page: pageNo.value,
    pageSize: pageSize.value,
    username: query.userName,
    fullname: query.name,
  });

  time.value = setTimeout(() => {
    loading.value = false;
  }, 5000);

  if (res.code === 200) {
    clearTimeout(time.value); // 清除定时器
    time.value = null;
    tableData.value = res.data.data;
    total.value = res.data.count;
    loading.value = false;
  }
};

onMounted(() => {
  get_userpage();
  store.getOrgAll();
});
</script>

<style lang="scss" scoped>
.user {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  .top {
    width: 100%;
    padding: 10px;
    background: #fff;
    display: flex;
    flex-wrap: wrap;
    gap: 30px;

    > div {
      width: calc((100% - (6 - 1) * 30px) / 4);
      height: 40px;
      display: flex;
      align-items: center;
      gap: 10px;
    }
  }
  .cont {
    flex: 1;
    padding: 10px;
    background: #fff;
    display: flex;
    flex-direction: column;
    .cont_top {
      width: 100%;
      height: 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .total_box {
        display: flex;
        > span {
          font-weight: bold;
          margin: 0 20px 0 5px;
        }
        p > span {
          margin: 0 5px;
          color: #09f;
        }
      }

      .operate {
        display: flex;
        gap: 10px;
      }
    }

    .cont_button {
      width: 100%;
      height: 40px;
      margin-top: 10px;
      display: flex;
      justify-content: end;
      align-items: center;
    }
  }
}

.total > span {
  margin: 0 5px;
}

.el-button {
  margin: 0;
}
</style>
